﻿using AutoMapper;
using MediatR;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;
using S.Organization.API.Application.Command.SheepFarmCommand;
using System.ComponentModel.DataAnnotations;

namespace S.Organization.API.Application.CommandHandler.SheepFarmCommandHandler
{
    public class UpdSheepFarmCommandHandler : IRequestHandler<UpdSheepFarmCommand, int>
    {
         private readonly IBaseRepository<SheepFarmSetting> _repository;
        private readonly IMapper mapper;

        public UpdSheepFarmCommandHandler(IBaseRepository<SheepFarmSetting> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(UpdSheepFarmCommand request, CancellationToken cancellationToken)
        {
            var list = await _repository.GetAllAsync( );
            var list1=list.Where(x=>x.SheepFarmId==request.SheepFarmId).FirstOrDefault();
            request.CreateDate = list1.CreateDate;
             request.CreateBy= list1.CreateBy;
            request.UpdateBy = "李四";
            request.UpdateDate = DateTime.Now;
            return await _repository.UpdateAsync(mapper.Map<SheepFarmSetting>(request));
        }


    }
}
